package com.eascs.fp.uc.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.eascs.app.dao.JdbcSession;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.common.util.StringUtil;
import com.eascs.fp.common.dao.BaseDao;
import com.eascs.fp.common.dto.MemberDTO;
import com.eascs.fp.common.entity.ApplyLoanEntity;
import com.eascs.fp.common.entity.MemberEntity;

@Repository
public class MemberDao extends BaseDao{
	private final static int MEMBER_STATUS=0;
	
	@Autowired
	@Qualifier("wxSession")
	protected JdbcSession wxSession;

	public MemberEntity queryMemberEntity(MemberEntity entity) {
		List<String> param=new ArrayList<>();
		StringBuffer sb=new StringBuffer();
		sb.append("select * from fp_user where status="+MEMBER_STATUS);
		if (StringUtils.isNotBlank(entity.getAccount())) {
			sb.append(" and account=?");
			param.add(entity.getAccount());
		}
		if (StringUtils.isNotBlank(entity.getMobile())) {
			sb.append(" and mobile=?");
			param.add(entity.getMobile());
		}
		if (StringUtils.isNotBlank(entity.getPassword())) {
			sb.append(" and password=?");
			param.add(entity.getPassword());
		}
		return wxSession.findEntity(sb.toString(), MemberEntity.class, param.toArray());
	}
	
	public boolean updateMemberEntity(MemberEntity entity) {
		entity.setUpdateTime(new Date());
		if (StringUtils.isBlank(entity.getMobile())) {
			return false;
		}
		List<Object> param=new ArrayList<>();
		StringBuffer sb=new StringBuffer();
		sb.append("update fp_user set update_time=?");
		param.add(new Date());
		if (StringUtils.isNotBlank(entity.getMobile())) {
			sb.append(",mobile=?");
			
			
			param.add(entity.getMobile());
		}
		if (StringUtils.isNotBlank(entity.getPassword())) {
			sb.append(",password=?");
			param.add(entity.getPassword());
		}
		sb.append(" where account=?");
		param.add(entity.getAccount());
		int result=wxSession.execute(sb.toString(), param.toArray());
		return result>0?true:false;
	}


	public MemberEntity insertMemberEntity(MemberEntity entity) {
		entity.setCreateTime(new Date());
		entity.setUpdateTime(new Date());
		int result = wxSession.insert(entity);
		if (result > 0) {
			return entity;
		}
		return null;
	}
	
	
	public PageResult<Object[]> queryMemberPage(MemberDTO dto) {
		StringBuffer sql = new StringBuffer();
		List<Object> paramsList = new ArrayList<>();
		sql.append(" SELECT f.id,f.mobile,f.create_time,(SELECT COUNT(1) FROM applay_info l WHERE l.telphone =f.mobile) as applyLoanRecord FROM fp_user f");
		sql.append(" WHERE 1=1");
		if (StringUtil.hasText(dto.getMobile())) {
			sql.append(" and f.mobile=?");
			paramsList.add(dto.getMobile());
		}
		if (dto.getStartTime() != null) {
			sql.append(" and f.create_time > ?");
			paramsList.add(dto.getStartTime());
		}
		if (dto.getEndTime() != null) {
			sql.append(" and f.create_time < ?");
			paramsList.add(dto.getEndTime());
		}
		sql.append(" ORDER BY f.create_time DESC");
		Object[] params = paramsList.toArray();
		SqlPageQuery<Object[]> pageQuery = new SqlPageQuery<Object[]>(Object[].class, dto.getPageSize(),
				dto.getPageIndex(), sql.toString());
		pageQuery.addParams(params);
		return wxSession.findDataWithPage(pageQuery);
	}
}
